<!--
 * @Description: 工程列表
 * @Author: charles
 * @Date: 2021-12-14 22:06:30
 * @LastEditors: HeAo
 * @LastEditTime: 2021-12-21 09:56:33
-->
<template>
  <div>
    <!-- 按钮区域 -->
    <div>
      <!-- 新增按钮 -->
      <el-button @click="openHandler" type="primary" size="small"
        >添加</el-button
      >
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="tableData.list" style="width: 100%" size="small">
        <el-table-column prop="name" label="工程名称"> </el-table-column>
        <el-table-column prop="serial_number" label="设备编号" width="120">
        </el-table-column>
        <el-table-column
          prop="type"
          label="工程类型"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="charge_name"
          label="区域经理"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="customer.realname"
          label="客户姓名"
          width="120px"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="customer.telephone"
          label="客户电话"
          width="120px"
          align="center"
        >
        </el-table-column>
        <el-table-column label="工程状态" width="80" align="center">
          <template v-slot="scope">
            <el-tag size="mini">{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template v-slot="scope">
            <el-button type="text" @click="toEditHandler(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" @click="toBindHandler(scope.row)"
              >绑定</el-button
            >
            <el-button type="text" @click="toEentHandler(scope.row)" 
              >结束</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框区域 -->
    <el-dialog
      @close="dialogCloseHandler"
      width="50%"
      :title="title"
      :visible.sync="visible"
    >
      {{ form }}
      <el-form :inline="true" ref="engineerForm" :model="form" :rules="rules">
        <el-form-item
          prop="serial_number"
          label="工程编号"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.serial_number"></el-input>
        </el-form-item>
        <el-form-item
          prop="name"
          label="工程名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          prop="address"
          label="工程地址"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item
          prop="longitude"
          label="经度"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.longitude"></el-input>
        </el-form-item>
        <el-form-item
          prop="latitude"
          label="纬度"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.latitude"></el-input>
        </el-form-item>
        <el-form-item
          prop="customer_name"
          label="客户姓名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.customer_name"></el-input>
        </el-form-item>
        <el-form-item
          prop="customer_telephone"
          label="客户电话"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.customer_telephone"></el-input>
        </el-form-item>
        <el-form-item
          prop="type"
          label="工程类型"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.type">
            <el-option label="园区" value="园区"></el-option>
            <el-option label="学校" value="学校"></el-option>
            <el-option label="医院" value="医院"></el-option>
            <el-option label="工地" value="工地"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          prop="charge_id"
          label="区域经理"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.charge_id">
            <el-option
              v-for="(item, index) in charges"
              :key="index"
              :label="item.realname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="commitHandler">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页组件 -->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="parseInt(tableData.page)"
      :page-size="parseInt(tableData.pageSize)"
      layout="total, sizes, prev, pager, next"
      :total="tableData.total"
      :page-sizes="[5, 10, 15, 20]"
    >
    </el-pagination>
  </div>
</template>
<script>
import { get, postJSON } from "@/utils/request";
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
      },
      tableData: {},
      visible: false,
      title: "",
      form: {},
      rules: {
        serial_number: [
          { required: true, message: "请输入工程编号", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入工程名称", trigger: "blur" }],
        address: [
          { required: true, message: "请输入工程地址", trigger: "blur" },
        ],
        longitude: [
          { required: true, message: "请输入工程经度", trigger: "blur" },
        ],
        latitude: [
          { required: true, message: "请输入工程纬度", trigger: "blur" },
        ],
        customer_name: [
          { required: true, message: "请输入客户姓名", trigger: "blur" },
        ],
        customer_telephone: [
          { required: true, message: "请输入客户电话", trigger: "blur" },
        ],
        type: [
          { required: true, message: "请选择工程类型", trigger: "change" },
        ],
        charge_id: [
          { required: true, message: "请选择区域经理", trigger: "change" },
        ],
      },
      formLabelWidth: "80px",
      charges: [],
    };
  },
  methods: {
    // 新增按钮点击事件
    openHandler() {
      this.title = "添加工程";
      this.form = {};
      this.visible = true;
    },
    // 编辑按钮点击事件
    toEditHandler(row) {
      this.form = {
        serial_number: row.serial_number,
        name: row.name,
        address:row.address,
        longitude:row.longitude,
        latitude:row.latitude,
        customer_name:row.customer_name,
        customer_telephone:row.customer_telephone,
        type:row.type,
        charge:row.charge_id,
      };
      // 修改模态框标题
      this.title = "修改工程";
      // 打开模态框
      this.visible = true;
    },
    //结束按钮点击事件
    toEentHandler(row){
       this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        get('/engineer/finish', { id }).then((res) => {
          this.pageQueryEngineers()
          this.$message.success('操作成功')
        })
      })
    },
    // 绑定按钮点击事件
    toBindHandler(row) {
      // vue中使用编程式导航实现页面跳转
      this.$router.push({
        path: "/am/engineer/Details",
        query: row,
      });
    },
    // page改变处理函数
    handleCurrentChange(page) {
      this.params.page = page;
      this.pageQueryEngineers();
    },
    // pageSize改变处理函数
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.pageQueryEngineers();
    },
    // 分页查询工程信息
    pageQueryEngineers() {
      get("/engineer/pageQuery", this.params).then((res) => {
        this.tableData = res.data;
      });
    },
    // 模态框关闭的回调函数
    dialogCloseHandler() {
      // 清空表单验证
      this.$refs["engineerForm"].resetFields();
    },
    // 表单的提交事件
    commitHandler() {
      this.$refs["engineerForm"].validate((valid) => {
        if (valid) {
          postJSON("/engineer/saveOrUpdate", this.form).then((res) => {
            // 刷新数据
            this.pageQueryEngineers();
            // 关闭模态框
            this.visible = false;
            // 提示成功信息
            this.$message({
              type: "success",
              message: "操作成功",
            });
          });
        } else {
          return false;
        }
      });
    },
    // 查询所有区域经理信息
    findAllCharges() {
      get("/user/pageQuery", {
        page: 1,
        pageSize: 100,
        role_id: 3,
      }).then((res) => {
        this.charges = res.data.list;
      });
    },
  },
  // vue生命周期的钩子函数
  created() {
    // 页面加载之前，请求工程数据
    this.pageQueryEngineers();
    // 查询所有区域经理
    this.findAllCharges();
  },
};
</script>
